// // This code is released under the BSD license.
namespace PugLib.Threading
{
	using System;
	using System.Threading;

	using Benchmarks;

	public delegate void ThreadWithStateCallback(AbstractThreadData td);

	public class ThreadWithState
	{
		// State information used in the task.

		// Delegate used to execute the callback method when the
		// task is complete.
		private readonly ThreadWithStateCallback _callback;

		private ListIntThreadData _threadState;

		public ThreadWithState(ListIntThreadData state, ThreadWithStateCallback callbackDelegate)
		{
			ThreadState = state;
			_callback = callbackDelegate;
		}

		public ListIntThreadData ThreadState
		{
			get { return _threadState; }
			set { _threadState = value; }
		}

		public void ThreadProc()
		{
			Thread.Sleep(1000);
			Int64 threadtotal = 0;
			for (int j = 0; j < Statics.Max; j++)
			{
				ThreadState.Items.ForEach(foo => { threadtotal += foo; });
			}
			ThreadState.Result = threadtotal;
			if (_callback != null)
			{
				_callback(ThreadState);
			}
		}
	}
}